<?php

/**
 * This is the model class for table "tbl_instrument_reserve".
 *
 * The followings are the available columns in table 'tbl_instrument_reserve':
 * @property integer $reserve_id
 * @property integer $elem_id
 * @property integer $user_id
 * @property string $start_date
 *
 * The followings are the available model relations:
 * @property InstrumentElem $elem
 * @property User $user
 */
class InstrumentReserve extends CActiveRecord
{
    const STATUS_WAIT_EXAMINE = 0;
    const STATUS_PASS_EXAMINE = 1;
    const STATUS_REFUSE_EXAMINE = 2;
    const STATUS_MAX_EXAMINE = 3;
    public $place;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return InstrumentReserve the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_instrument_reserve';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('elem_id, user_id, start_date, instrument_id', 'required'),
			array('elem_id, user_id, status, instrument_id', 'numerical', 'integerOnly'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('reserve_id, elem_id, user_id, start_date, status,instrument_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'instrumentElem'=> array(self::BELONGS_TO, 'InstrumentElem', 'elem_id'),
			'instrument'=> array(self::BELONGS_TO, 'Instrument', 'instrument_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
		);
    }
    public function getStatusOptions()
    {
        return array(
            self::STATUS_WAIT_EXAMINE => '待审核',
            self::STATUS_PASS_EXAMINE => '已通过',
            self::STATUS_REFUSE_EXAMINE => '未通过',
            self::STATUS_MAX_EXAMINE => '已满人',
        );
    }
    public function getStatusText()
    {
        if(self::STATUS_WAIT_EXAMINE == $this->status)
        {
            return '待审核';
        }
        if(self::STATUS_PASS_EXAMINE == $this->status)
        {
            return '已通过';
        }
        if(self::STATUS_REFUSE_EXAMINE == $this->status)
        {
            return '未通过';
        }
        if(self::STATUS_MAX_EXAMINE == $this->status)
        {
            return '已满人';
        }
    }
    public function getFormatTime()
    {
        $start = $this->start_date;
        $end = date('Y-m-d H:i:s', strtotime('+ 1 hours', strtotime($start)));
        return $start.' 到 '.$end;
    }

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'reserve_id' => '预定编号',
			'elem_id' => '资产编号',
			'user_id' => '预约用户',
			'status' => '审核',
			'start_date' => '预定时间',
			'instrument_id' => '仪器名称',
			'place' => '放置地点',
		);
	}
    public function getInstrumentID()
    {
        return $this->instrumentElem->instrument_id;
    }
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */

    public function searchMyRecordPend()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('reserve_id',$this->reserve_id);
		$criteria->compare('elem_id',$this->elem_id);
		$criteria->compare('user_id',yii::app()->user->id);
		$criteria->compare('start_date',$this->start_date,true);
        $criteria->addCondition('start_date > \''.date('Y-m-d H:i:s', time()).'\'');
		$criteria->order = 'start_date ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    public function searchMyRecordPass()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('reserve_id',$this->reserve_id);
		$criteria->compare('elem_id',$this->elem_id);
		$criteria->compare('user_id',yii::app()->user->id);
		$criteria->compare('start_date',$this->start_date,true);
        $criteria->addCondition('start_date <= \''.date('Y-m-d H:i:s', time()).'\'');
		$criteria->order = 'start_date ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function searchPend($model,$status)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

        $criteria=new CDbCriteria;

        if($this->user_id !== NULL && $this->user_id !== "")
        {
            $user = User::model()->findByAttributes(array('user_name'=>$this->user_id));
            if($user === NULL)
            {
                $criteria->compare('user_id','-1');
            }
            else
            {
                $criteria->compare('user_id',$user->user_id);
            }
        }
        if($this->elem_id !== NULL && $this->elem_id !== "")
        {
            $elem = InstrumentElem::model()->findByAttributes(array('asset_number'=>$this->elem_id));
            if($elem === NULL)
            {
                $criteria->compare('elem_id','-1');
            }
            else
            {
                $criteria->compare('elem_id',$elem->elem_id);
            }
        }
		$criteria->compare('reserve_id',$this->reserve_id);
		$criteria->compare('start_date',$this->start_date,true);
        if($status == InstrumentReserve::STATUS_WAIT_EXAMINE)
        {
            $criteria->compare('status',InstrumentReserve::STATUS_WAIT_EXAMINE, true);
        }
        else
        {
            $criteria->addCondition('status > '.InstrumentReserve::STATUS_WAIT_EXAMINE);
        }
        $criteria->compare('instrument_id', $model->instrument_id, true);
        $criteria->addCondition('start_date > \''.date('Y-m-d H:i:s', time()).'\'');
        $criteria->order = 'start_date ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
    public function searchPass($model)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
        if($this->user_id !== NULL && $this->user_id !== "")
        {
            $user = User::model()->findByAttributes(array('user_name'=>$this->user_id));
            if($user === NULL)
            {
                $criteria->compare('user_id','-1');
            }
            else
            {
                $criteria->compare('user_id',$user->user_id);
            }
        }
        if($this->elem_id !== NULL && $this->elem_id !== "")
        {
            $elem = InstrumentElem::model()->findByAttributes(array('asset_number'=>$this->elem_id));
            if($elem === NULL)
            {
                $criteria->compare('elem_id','-1');
            }
            else
            {
                $criteria->compare('elem_id',$elem->elem_id);
            }
        }
		$criteria->compare('reserve_id',$this->reserve_id);
		$criteria->compare('start_date',$this->start_date,true);
        $criteria->compare('instrument_id', $model->instrument_id, true);
		$criteria->order = 'start_date DESC';
        $criteria->addCondition('start_date <= \''.date('Y-m-d H:i:s', time()).'\'');
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}
